package com.webserver6.core;

import com.webserver6.http.HttpServletResponse;
import com.webserver6.http.HttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.URISyntaxException;

public class ClientHandler implements Runnable {
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket = socket;
    }
    public void run(){
        try {
            HttpServletRequest request = new HttpServletRequest(socket);
            HttpServletResponse response = new HttpServletResponse(socket);
            DispatcherServlet servlet = DispatcherServlet.getInstance();
            servlet.service(request,response);
            response.response();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws URISyntaxException {
        File rootDir = new File(
                ClientHandler.class.getClassLoader().getResource(".").toURI()
        );
        File staticDir = new File(rootDir,"static");
        System.out.println(1);
        File file = new File("index.html");
        System.out.println("文件是否存在："+file.exists());
        System.out.println(rootDir);
    }
}
